<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 反转从位置m到n的链表。请使用一趟扫描完成反转。(1 ≤m≤n≤ 链表长度)

        // 输入: 1 -> 2 -> 3 -> 4 -> 5 -> NULL, m = 2, n = 4
        // 输出: 1 -> 4 -> 3 -> 2 -> 5 -> NULL

        function reverseBetween(head, m, n) {
            var count = n - m;
            var dummy = {};
            dummy.next = head;

            var p = dummy;
            for (var i = 0; i < m - 1; i++) {
                p = p.next;
            }
            var front = p;
            var prev = tail = p.next;

            var cur = prev.next;

            for (var i = 0; i < count; i++) {
                var next = cur.next;
                cur.next = prev;
                prev = cur;
                cur = next;
            }
            front.next = prev;
            tail.next = cur;
            return dummy.next;


        }

        // 第二遍 
        // abcdef

        // 不要假头,遍历找下一个节点,从 1开始
        function reverseBetween(head, m, n) {
            var dummy = {};
            dummy.next = head;

            var front = dummy;
            for (var i = 0; i < m - 1; i++) {
                front = front.next;
            }
            var prev = tail = front.next;
            var cur = prev.next;

            var count = n - m;
            for (var i = 0; i < count; i++) {
                var next = cur.next;
                cur.next = prev;
                prev = cur;
                cur = next;
            }

            front.next = prev;
            tail.next = cur;

            return dummy.next;



        }
    </script>
</body>

</html>